package com.xiaoyang;

import com.xiaoyang.bean.Car;
import com.xiaoyang.bean.User;
import com.xiaoyang.conf.MyConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.util.Arrays;

/**
 * @author lichenyang
 * @create 2021-07-11 10:14
 *
 * @SpringBootApplication: 表示这是一个 springboot应用
 * 标注了 @SpringBootApplication 注解的类 为 springboot 的主程序类
 *
 * springboot 注入原生 Servlet 时 使用 @ServletComponentScan(basePackages = "com.xiaoyang.servlet")
 * 表示 扫描 com.xiaoyang.servlet 包下的 @WebServlet(urlPatterns = "/myServlet") 注解
 */
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.xiaoyang")
@MapperScan("com.xiaoyang.mapper")
//@ServletComponentScan(basePackages = "com.xiaoyang.servlet")
//@SpringBootApplication()
public class MainStarter {

    public static void main(String[] args) {
        // 可以返回容器中的所有组件
        ConfigurableApplicationContext run = SpringApplication.run(MainStarter.class, args);

        // @Condition注解演示
//        boolean pet = run.containsBean("pet");

        // 演示 @ConfigurationProperties("car") 注解
        Car car = run.getBean(Car.class);
        System.out.println(car);


        MyConfig myConfig = run.getBean(MyConfig.class);

        //如果 @Configuration(proxyBeanMethods = false), 不管容器中是否存在相应的组件, 都会重新创建
        System.out.println(myConfig);
        User user = myConfig.user();
        User user2 = myConfig.user();
        System.out.println(user == user2);

/*
        //如果 @Configuration(proxyBeanMethods = true), 则 springboot 会总是检查 当前组件在 spring 容器中是否存在, 如果存在就直接拿
        System.out.println(myConfig);
        User user = myConfig.user();
        User user2 = myConfig.user();
        System.out.println(user == user2);
*/

        // 查看容器中所有组件的名称
//        Arrays.stream(run.getBeanDefinitionNames()).forEach(System.out::println);

    }

}
